package com.tomdd.controller;

import com.tomdd.model.entity.Category;
import com.tomdd.model.req.CategoryReq;
import com.tomdd.model.resp.Result;
import com.tomdd.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章分类控制器
 *
 * @author zx
 * @date 2024-07-18 21:24
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     *
     * @param category 分类
     * @return 响应结果
     */
    @PostMapping
    public Result add(@RequestBody @Validated(CategoryReq.Add.class) CategoryReq category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 更具用户ID查询文章分类
     *
     * @return 文章分类
     */
    @GetMapping
    public Result<List<Category>> list() {
        List<Category> cs = categoryService.listByUserId();
        return Result.success(cs);
    }

    /**
     * 根据id查询分类信息
     *
     * @param id 文章分类id
     * @return
     */
    @GetMapping("/detail")
    public Result<Category> detail(@RequestParam Integer id) {
        Category c = categoryService.findById(id);
        return Result.success(c);
    }

    /**
     * 根据文章分类id修改分类信息
     *
     * @param categoryReq 分类
     * @return 响应结果
     */
    @PutMapping
    public Result update(@RequestBody @Validated(CategoryReq.Update.class) CategoryReq categoryReq) {
        categoryService.updateCategory(categoryReq);
        return Result.success();
    }

    /**
     * 根据文章分类id删除分类信息
     *
     * @param id 文章分类id
     * @return 响应结果
     */
    @DeleteMapping
    public Result delete(Integer id) {
        categoryService.removeById(id);
        return Result.success();
    }
}
